import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class HtmlParseTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		 //File input = new File("./iframe1.html"); 
		 String targetUrl = "./iframe5.html";
		 //Document doc = Jsoup.parse(input, "UTF-8"); 
		 Document doc = Jsoup.parse(getText(targetUrl).toString()); 
		 //Document doc = Jsoup.connect("http://www.oschina.net/").get(); 
		 Element content = doc.getElementById("adHolder"); 
		 
		 //image target url
		 Elements links = content.getElementsByTag("a"); 
		 System.out.println(links.size());
		 for (Element link : links) { 
		  String linkHref = link.attr("href"); 
		  System.out.println("Target Url:"+linkHref);
		 } 
		 
		 //image width height
		 Elements imgs = content.getElementsByTag("img"); 
		 System.out.println(imgs.size());
		 for (Element img : imgs) { 
		  String imgsrc = img.attr("src"); 
		  System.out.println("Src:"+imgsrc);
		  String imgwidth = img.attr("width"); 
		  System.out.println("Width:"+imgwidth);
		  String imgheight = img.attr("height"); 
		  System.out.println("Height:"+imgheight);
		 }
		 
		 //flash target url
		 Elements flashs_target = doc.select("noscript");
		 System.out.println(flashs_target.text());
		 Document flashTargetDoc = Jsoup.parse(flashs_target.text());
		 Elements flash_target = flashTargetDoc.select("a");
		 System.out.println(flash_target.size());
		 for (Element flash : flash_target) { 
			  System.out.println("Enter");
			  String flashHref = flash.attr("href"); 
			  System.out.println("Flash Target Url:"+flashHref);
			 }
		 
		 //flash src wdith and height
		 Elements flashs_vars = doc.select("embed");
		 System.out.println(flashs_vars.size());
		 for (Element flash : flashs_vars) { 
			  System.out.println("Enter");
			  String flashHref = flash.attr("src"); 
			  System.out.println("Flash Src:"+flashHref);
			  String flashQuality = flash.attr("quality");
			  System.out.println("Flash Quality:"+flashQuality);
			  String flashWidth = flash.attr("width"); 
			  System.out.println("Flash Width:"+flashWidth);
			  String flashHeight = flash.attr("height"); 
			  System.out.println("Flash Height:"+flashHeight);
			 }
		 

	}
	
	private static CharSequence getText(String targetUrl){
		StringBuilder sb = new StringBuilder();
		Scanner scanner;
		try {
			scanner = new Scanner(new File(targetUrl));
			while(scanner.hasNextLine()){
				String str=scanner.nextLine();
				sb.append(str);
			}	
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}

}
